package cn.hyxy.day02;


import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;

public class Demo01_HDFS {
	public static void main(String[] args) throws Exception {
		
		Configuration config=new Configuration();
		//config.set("fs.defaultFS", "hdfs://hadoop31:8020");
		
		URI uri=URI.create("hdfs://hadoop31:8020");
		//FileSystem fs=FileSystem.get(config);
		FileSystem fs=FileSystem.get(uri, config, "abc");
		//FileSystem fs= FileSystem.newInstance(uri, config, "abc");
		
		
		RemoteIterator<LocatedFileStatus> it=fs.listLocatedStatus(new Path("/"));
//		while (it.hasNext()) {
//			LocatedFileStatus file=it.next();
//			String name=file.getPath().toString();
//			System.out.println(name);			
//		}
		
		
		while (it.hasNext()) {
			LocatedFileStatus file=it.next();
			
			String d="-";
			if (file.isDirectory()) {
				d="d";
			}
			
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String p=file.getPermission().toString();
			short rep=file.getReplication();
			String o=file.getOwner();
			String g=file .getGroup();
			long size=file.getLen();
			String time=sdf.format(new Date(file.getModificationTime()));			
			
			String path=file.getPath().toString();
			System.out.println(d+p+""+rep+""+o+""+g+""+size+""+time+""+path);			
		}
		fs.close();
	}
}
